package com.ramblingcoder.bc2.server.tool.update;

import java.io.IOException;
import java.net.Socket;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.ramblingcoder.bc2.server.tool.update.R;
import com.ramblingcoder.bc2.server.tool.update.commands.SupportingFunctions;
import com.ramblingcoder.bc2.server.tool.update.exceptions.BanListFull;
import com.ramblingcoder.bc2.server.tool.update.exceptions.InvalidArguments;
import com.ramblingcoder.bc2.server.tool.update.exceptions.InvalidForceKill;
import com.ramblingcoder.bc2.server.tool.update.exceptions.InvalidPlayerName;
import com.ramblingcoder.bc2.server.tool.update.exceptions.InvalidSquadId;
import com.ramblingcoder.bc2.server.tool.update.exceptions.InvalidTeamId;
import com.ramblingcoder.bc2.server.tool.update.exceptions.LogInRequired;
import com.ramblingcoder.bc2.server.tool.update.exceptions.PlayerNotDead;
import com.ramblingcoder.bc2.server.tool.update.exceptions.PlayerNotFound;
import com.ramblingcoder.bc2.server.tool.update.exceptions.SetSquadFailed;
import com.ramblingcoder.bc2.server.tool.update.exceptions.SetTeamFailed;
import com.ramblingcoder.bc2.server.tool.update.exceptions.SoldierNotAlive;
import com.ramblingcoder.bc2.server.tool.update.objects.Ban;
import com.ramblingcoder.bc2.server.tool.update.objects.InfoShare;
import com.ramblingcoder.bc2.server.tool.update.objects.Map;
import com.ramblingcoder.bc2.server.tool.update.objects.Player;

public class PlayerView extends Activity {
	//Initializer-----\
	private static Map map;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.playerview);
        
		try {
			populate();
		} catch (IOException e) {
			SupportingFunctions.showError(this, e.getMessage(), new Intent(this, BC2ServerChoice.class));
		} catch (ArrayIndexOutOfBoundsException e) {
			SupportingFunctions.showError(this, "Error parsing player's list. Array index is broken.", new Intent(this, BC2ServerChoice.class));
		} catch (LogInRequired e) {
			SupportingFunctions.showError(this, e.getMessage(), new Intent(this, BC2ServerChoice.class));
		} catch (InvalidArguments e) {
			SupportingFunctions.showError(this, e.getMessage(), new Intent(this, BC2ServerChoice.class));
		}
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
	    menu.add(1, 5, 0, "Main Menu");
	    menu.add(1, 0, 0, "Refresh");
	    menu.add(1, 1, 0, "Bans View");
	    menu.add(1, 2, 0, "Map View");
	    menu.add(1, 3, 0, "Settings View");
	    menu.add(1, 4, 0, "Chat View");
	    menu.add(1, 6, 0, "Reserved View");
	    return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	    case 5:
	    	finish();
	    	startActivity(new Intent(this, BC2ServerChoice.class));
	    	break;
	    case 0:
	    	finish();
	    	startActivity(new Intent(this, PlayerView.class));
	    	break;
	    case 1:	
	    	finish();
	    	startActivity(new Intent(this, BanView.class));
	    	break;
	    case 2:
	    	finish();
	    	startActivity(new Intent(this, MapView.class));
	    	break;
	    case 3:
	    	finish();
	    	startActivity(new Intent(this, SettingsView.class));
	    	break;
	    case 4:
	    	finish();
	    	startActivity(new Intent(this, ChatView.class));
	    	break;
	    case 6:
	    	finish();
	    	startActivity(new Intent(this, ReservedView.class));
	    	break;
	    }
	    return true;
	}

	private void populate() throws IOException, LogInRequired, InvalidArguments {		
		map = Map.currentMap(InfoShare.getCommand());
		
		Player[] players = Player.listPlayers(InfoShare.getCommand());
		
		int us = 0, usa = 0, usb = 0, usc = 0, usd = 0, use = 0, usf = 0, usg = 0, ush = 0;
		int ru = 0, rua = 0, rub = 0, ruc = 0, rud = 0, rue = 0, ruf = 0, rug = 0, ruh = 0;
		
		int t1=0, t2=0, t3=0, t4=0;
		LinearLayout layout = (LinearLayout) findViewById(R.id.playerviewlayout);
		
		int count = 0;
		for (int index = 0; index < players.length; index++){
			Player player = players[index];
			
			if (player.getTeam() == 1){
				layout.addView(addPlayerView(player, count));
				count++;
				
				t1++;
				us++;
				
				switch(player.getSquad()){
					case 1: usa++;
						break;
					case 2: usb++;
						break;
					case 3: usc++;
						break;
					case 4: usd++;
						break;
					case 5: use++;
						break;
					case 6: usf++;
						break;
					case 7: usg++;
						break;
					case 8: ush++;
						break;
				}
			}
		}
		
		for (int index = 0; index < players.length; index++){
			Player player = players[index];
			
			if (player.getTeam() == 2){
				layout.addView(addPlayerView(player, count));
				count++;

				t2++;
				ru++;
				
				switch(player.getSquad()){
					case 1: rua++;
						break;
					case 2: rub++;
						break;
					case 3: ruc++;
						break;
					case 4: rud++;
						break;
					case 5: rue++;
						break;
					case 6: ruf++;
						break;
					case 7: rug++;
						break;
					case 8: ruh++;
						break;
				}
			}
		}
		
		for (int index = 0; index < players.length; index++){
			Player player = players[index];
			
			if (player.getTeam() == 3){
				layout.addView(addPlayerView(player, count));
				count++;

				t3++;
			}
		}
		
		for (int index = 0; index < players.length; index++){
			Player player = players[index];
			
			if (player.getTeam() == 4){
				layout.addView(addPlayerView(player, count));
				count++;

				t4++;
			}
		}
		
		for (int index = 0; index < players.length; index++){
			Player player = players[index];
			
			if (player.getTeam() == 0){
				layout.addView(addPlayerView(player, count));
				count++;
			}
		}
		
		if (map.getMapType() == Map.TYPE_CONQUEST){
			((TextView) findViewById(R.id.unitedstatescount)).setText(""+us);
			((TextView) findViewById(R.id.russiacount)).setText(""+ru);
		} else if (map.getMapType() == Map.TYPE_RUSH || map.getMapType() == Map.TYPE_SQUADRUSH){
			((TextView) findViewById(R.id.team1)).setText("Attackers");
			((TextView) findViewById(R.id.team2)).setText("Defenders");
			((TextView) findViewById(R.id.unitedstatescount)).setText(""+us);
			((TextView) findViewById(R.id.russiacount)).setText(""+ru);
		} else if (map.getMapType() == Map.TYPE_SQUADDEATHMATCH){
			((TextView) findViewById(R.id.team1)).setText("Player Count: ");
			((TextView) findViewById(R.id.team1)).setGravity(Gravity.RIGHT);
			((TextView) findViewById(R.id.unitedstatescount)).setText(""+players.length);

			((LinearLayout) findViewById(R.id.squadlabels)).setVisibility(View.GONE);
			((LinearLayout) findViewById(R.id.russia)).setVisibility(View.GONE);
		}
		
		((TextView) findViewById(R.id.unitedstatesa)).setText(""+usa);
		((TextView) findViewById(R.id.unitedstatesb)).setText(""+usb);
		((TextView) findViewById(R.id.unitedstatesc)).setText(""+usc);
		((TextView) findViewById(R.id.unitedstatesd)).setText(""+usd);
		((TextView) findViewById(R.id.unitedstatese)).setText(""+use);
		((TextView) findViewById(R.id.unitedstatesf)).setText(""+usf);
		((TextView) findViewById(R.id.unitedstatesg)).setText(""+usg);
		((TextView) findViewById(R.id.unitedstatesh)).setText(""+ush);
		
		((TextView) findViewById(R.id.russiaa)).setText(""+rua);
		((TextView) findViewById(R.id.russiab)).setText(""+rub);
		((TextView) findViewById(R.id.russiac)).setText(""+ruc);
		((TextView) findViewById(R.id.russiad)).setText(""+rud);
		((TextView) findViewById(R.id.russiae)).setText(""+rue);
		((TextView) findViewById(R.id.russiaf)).setText(""+ruf);
		((TextView) findViewById(R.id.russiag)).setText(""+rug);
		((TextView) findViewById(R.id.russiah)).setText(""+ruh);

		 if (map.getMapType() == Map.TYPE_SQUADDEATHMATCH){
			((TextView) findViewById(R.id.unitedstatesa)).setText("Team 1: "+t1);
			((TextView) findViewById(R.id.unitedstatesb)).setText("Team 2: "+t2);
			((TextView) findViewById(R.id.unitedstatesc)).setText("Team 3: "+t3);
			((TextView) findViewById(R.id.unitedstatesd)).setText("Team 4: "+t4);
			
			((TextView) findViewById(R.id.unitedstatese)).setVisibility(View.GONE);
			((TextView) findViewById(R.id.unitedstatesf)).setVisibility(View.GONE);
			((TextView) findViewById(R.id.unitedstatesg)).setVisibility(View.GONE);
			((TextView) findViewById(R.id.unitedstatesh)).setVisibility(View.GONE);
		}
		
		if (map.getMapType() == Map.TYPE_SQUADRUSH){
			((TextView) findViewById(R.id.unitedstatesb)).setText("-");
			((TextView) findViewById(R.id.unitedstatesc)).setText("-");
			((TextView) findViewById(R.id.unitedstatesd)).setText("-");
			((TextView) findViewById(R.id.unitedstatese)).setText("-");
			((TextView) findViewById(R.id.unitedstatesf)).setText("-");
			((TextView) findViewById(R.id.unitedstatesg)).setText("-");
			((TextView) findViewById(R.id.unitedstatesh)).setText("-");
			
			((TextView) findViewById(R.id.russiab)).setText("-");
			((TextView) findViewById(R.id.russiac)).setText("-");
			((TextView) findViewById(R.id.russiad)).setText("-");
			((TextView) findViewById(R.id.russiae)).setText("-");
			((TextView) findViewById(R.id.russiaf)).setText("-");
			((TextView) findViewById(R.id.russiag)).setText("-");
			((TextView) findViewById(R.id.russiah)).setText("-");
		}
		
	}

	private LinearLayout addPlayerView(final Player player, int index) throws IOException, LogInRequired, InvalidArguments {
		LinearLayout playerLayout = new LinearLayout(this);
		playerLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		playerLayout.setPadding(3,3,3,3);
		playerLayout.setGravity(Gravity.CENTER|Gravity.LEFT);
		
		if (index % 2 == 1) playerLayout.setBackgroundColor(Color.parseColor("#333333"));
		else playerLayout.setBackgroundColor(Color.BLACK);
		
		if (map.getMapType() == Map.TYPE_CONQUEST){
			ImageView team = new ImageView(this);
			team.setLayoutParams(new LayoutParams(45, LayoutParams.WRAP_CONTENT));
			
			if (player.getTeam() == 1) team.setImageResource(R.drawable.american);
			else if (player.getTeam() == 2) team.setImageResource(R.drawable.russian);
			else  team.setImageResource(R.drawable.neutral);

			playerLayout.addView(team);
		} else if (map.getMapType() == Map.TYPE_RUSH || map.getMapType() == Map.TYPE_SQUADRUSH){
			ImageView team = new ImageView(this);
			team.setLayoutParams(new LayoutParams(45, LayoutParams.WRAP_CONTENT));
			
			if (player.getTeam() == 1) team.setImageResource(R.drawable.attacker);
			else if (player.getTeam() == 2) team.setImageResource(R.drawable.defender);
			else  team.setImageResource(R.drawable.neutral);
			
			playerLayout.addView(team);
			
		} else if (map.getMapType() == Map.TYPE_SQUADDEATHMATCH){
			TextView team = new TextView(this);
			team.setLayoutParams(new LayoutParams(45, LayoutParams.WRAP_CONTENT));
			
			team.setText(player.getTeam()+"");

			playerLayout.addView(team);
		}
		
		int textSize = 16;
		
		TextView tag = new TextView(this);
		tag.setText(player.getTag());
		tag.setWidth(70);
		tag.setTextSize(textSize);
		
		TextView name = new TextView(this);
		name.setText(player.getName());
		name.setTextSize(textSize);
		name.setSingleLine();
		name.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));

		TextView kills = new TextView(this);
		kills.setText(player.getKills()+"");
		kills.setWidth(20);
		kills.setTextSize(textSize);

		TextView deaths = new TextView(this);
		deaths.setText(player.getDeaths()+"");
		deaths.setWidth(20);
		deaths.setTextSize(textSize);
		
		TextView ping = new TextView(this);
		ping.setText(player.getPing()+"");
		ping.setWidth(30);
		ping.setTextSize(textSize);
		if (player.getPing() > 300)
			ping.setTextColor(Color.RED);
		else if (player.getPing() > 200)
			ping.setTextColor(Color.YELLOW);
		else if (player.getPing() >= 0)
			ping.setTextColor(Color.GREEN);
		else if (player.getPing() < 0)
			ping.setTextColor(Color.YELLOW);
		
		playerLayout.addView(tag);
		playerLayout.addView(name);
		playerLayout.addView(kills);
		playerLayout.addView(deaths);
		playerLayout.addView(ping);

		if (player.getKills() > player.getDeaths())
			kills.setTextColor(Color.GREEN);
		else if (player.getKills() < player.getDeaths())
			deaths.setTextColor(Color.RED);
		else{
			kills.setTextColor(Color.YELLOW);
			deaths.setTextColor(Color.YELLOW);
		}
		
		playerLayout.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				kickPlayer(player);
			}
		});
		
		return playerLayout;
	}

	private void kickPlayer(final Player player) {
		final Dialog dialog = new Dialog(PlayerView.this);
		dialog.setContentView(R.layout.playerkickban);
		dialog.setTitle("Selected "+player.getName()+"");
		
		final RadioButton kick = (RadioButton) dialog.findViewById(R.id.kick);
		final RadioButton ban = (RadioButton) dialog.findViewById(R.id.ban);
		final RadioButton move = (RadioButton) dialog.findViewById(R.id.move);
		final RadioButton kill = (RadioButton) dialog.findViewById(R.id.kill);
		
		final LinearLayout banOptions = (LinearLayout) dialog.findViewById(R.id.banoptions);
		final RadioButton name = (RadioButton) dialog.findViewById(R.id.name);
		final RadioButton guid = (RadioButton) dialog.findViewById(R.id.guid);
		final RadioButton permanent = (RadioButton) dialog.findViewById(R.id.permanent);
		final RadioButton round = (RadioButton) dialog.findViewById(R.id.round);
		final EditText reason = (EditText) dialog.findViewById(R.id.reason);

		final LinearLayout moveOptions = (LinearLayout) dialog.findViewById(R.id.moveoptions);
		final RadioButton us = (RadioButton) dialog.findViewById(R.id.us);
		final RadioButton ru = (RadioButton) dialog.findViewById(R.id.ru);
		final RadioButton t3 = (RadioButton) dialog.findViewById(R.id.team3);
		final RadioButton t4 = (RadioButton) dialog.findViewById(R.id.team4);
		final RadioButton a = (RadioButton) dialog.findViewById(R.id.a);
		final RadioButton b = (RadioButton) dialog.findViewById(R.id.b);
		final RadioButton c = (RadioButton) dialog.findViewById(R.id.c);
		final RadioButton d = (RadioButton) dialog.findViewById(R.id.d);
		final RadioButton e = (RadioButton) dialog.findViewById(R.id.e);
		final RadioButton f = (RadioButton) dialog.findViewById(R.id.f);
		final RadioButton g = (RadioButton) dialog.findViewById(R.id.g);
		final RadioButton h = (RadioButton) dialog.findViewById(R.id.h);
		
		ScrollView squads = (ScrollView) dialog.findViewById(R.id.squads);

		final Button confirm = (Button) dialog.findViewById(R.id.confirm);
		Button cancel = (Button) dialog.findViewById(R.id.cancel);
		
		banOptions.setVisibility(View.GONE);
		moveOptions.setVisibility(View.GONE);
		t3.setVisibility(View.GONE);
		t4.setVisibility(View.GONE);

		if (map.getMapType() == Map.TYPE_SQUADDEATHMATCH){
			us.setText("1");
			ru.setText("2");
			t3.setText("3");
			t4.setText("4");
			
			t3.setVisibility(View.VISIBLE);
			t4.setVisibility(View.VISIBLE);

			squads.setVisibility(View.GONE);
		} else if (map.getMapType() == Map.TYPE_SQUADRUSH){
			us.setText("Attacker");
			ru.setText("Defender");
			
			squads.setVisibility(View.GONE);
		} else if (map.getMapType() == Map.TYPE_RUSH){
			us.setText("Attacker");
			ru.setText("Defender");
		}
		
		ban.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (ban.isChecked()){
					banOptions.setVisibility(View.VISIBLE);
					moveOptions.setVisibility(View.GONE);
				} else if (move.isChecked()){
					banOptions.setVisibility(View.GONE);
					moveOptions.setVisibility(View.VISIBLE);
				} else if (kick.isChecked()){
					banOptions.setVisibility(View.GONE);
					moveOptions.setVisibility(View.GONE);
				} else if (kill.isChecked()){
					banOptions.setVisibility(View.GONE);
					moveOptions.setVisibility(View.GONE);
				}
				confirm.setEnabled(true);
			}
		});
		move.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (ban.isChecked()){
					banOptions.setVisibility(View.VISIBLE);
					moveOptions.setVisibility(View.GONE);
				} else if (move.isChecked()){
					banOptions.setVisibility(View.GONE);
					moveOptions.setVisibility(View.VISIBLE);
				} else if (kick.isChecked()){
					banOptions.setVisibility(View.GONE);
					moveOptions.setVisibility(View.GONE);
				} else if (kill.isChecked()){
					banOptions.setVisibility(View.GONE);
					moveOptions.setVisibility(View.GONE);
				}
				confirm.setEnabled(true);
			}
		});
		kick.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (ban.isChecked()){
					banOptions.setVisibility(View.VISIBLE);
					moveOptions.setVisibility(View.GONE);
				} else if (move.isChecked()){
					banOptions.setVisibility(View.GONE);
					moveOptions.setVisibility(View.VISIBLE);
				} else if (kick.isChecked()){
					banOptions.setVisibility(View.GONE);
					moveOptions.setVisibility(View.GONE);
				} else if (kill.isChecked()){
					banOptions.setVisibility(View.GONE);
					moveOptions.setVisibility(View.GONE);
				}
				confirm.setEnabled(true);
			}
		});
		kill.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (ban.isChecked()){
					banOptions.setVisibility(View.VISIBLE);
					moveOptions.setVisibility(View.GONE);
				} else if (move.isChecked()){
					banOptions.setVisibility(View.GONE);
					moveOptions.setVisibility(View.VISIBLE);
				} else if (kick.isChecked()){
					banOptions.setVisibility(View.GONE);
					moveOptions.setVisibility(View.GONE);
				} else if (kill.isChecked()){
					banOptions.setVisibility(View.GONE);
					moveOptions.setVisibility(View.GONE);
				}
				confirm.setEnabled(true);
			}
		});
		
		confirm.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Socket socket = InfoShare.getCommand();

				try {
					if (kick.isChecked()) {
						Ban.kickPlayer(socket, player);

					} else if (kill.isChecked()) {
						Player.killPlayer(socket, player);
						
					} else if (ban.isChecked()) {
						if (name.isChecked()){
							if (permanent.isChecked())
								Ban.ban(socket, "name", player.getName(), "perm", reason.getText().toString());
							else if (round.isChecked())
								Ban.ban(socket, "name", player.getName(), "round", reason.getText().toString());
						} else if (guid.isChecked()){
							if (permanent.isChecked())
								Ban.ban(socket, "guid", player.getGuid(), "perm", reason.getText().toString());
							else if (round.isChecked())
								Ban.ban(socket, "guid", player.getGuid(), "round", reason.getText().toString());
						}
						
					} else if (move.isChecked()) {
						if (map.getMapType() == Map.TYPE_RUSH || map.getMapType() == Map.TYPE_CONQUEST){
							if (us.isChecked()){
								if (a.isChecked())
									Player.move(socket, player.getName(), "1", "1", true);
								else if (b.isChecked())
									Player.move(socket, player.getName(), "1", "2", true);
								else if (c.isChecked())
									Player.move(socket, player.getName(), "1", "3", true);
								else if (d.isChecked())
									Player.move(socket, player.getName(), "1", "4", true);
								else if (e.isChecked())
									Player.move(socket, player.getName(), "1", "5", true);
								else if (f.isChecked())
									Player.move(socket, player.getName(), "1", "6", true);
								else if (g.isChecked())
									Player.move(socket, player.getName(), "1", "7", true);
								else if (h.isChecked())
									Player.move(socket, player.getName(), "1", "8", true);
							} else if (ru.isChecked()){ 
								if (a.isChecked())
									Player.move(socket, player.getName(), "2", "1", true);
								else if (b.isChecked())
									Player.move(socket, player.getName(), "2", "2", true);
								else if (c.isChecked())
									Player.move(socket, player.getName(), "2", "3", true);
								else if (d.isChecked())
									Player.move(socket, player.getName(), "2", "4", true);
								else if (e.isChecked())
									Player.move(socket, player.getName(), "2", "5", true);
								else if (f.isChecked())
									Player.move(socket, player.getName(), "2", "6", true);
								else if (g.isChecked())
									Player.move(socket, player.getName(), "2", "7", true);
								else if (h.isChecked())
									Player.move(socket, player.getName(), "2", "8", true);
							}
							
						} else if (map.getMapType() == Map.TYPE_SQUADRUSH){
							if (us.isChecked())
								Player.move(socket, player.getName(), "1", "1", true);
							else if (ru.isChecked())
								Player.move(socket, player.getName(), "2", "1", true);
							
						} else if (map.getMapType() == Map.TYPE_SQUADDEATHMATCH){
							if (us.isChecked())
								Player.move(socket, player.getName(), "1", "1", true);
							else if (ru.isChecked())
								Player.move(socket, player.getName(), "2", "1", true);
							else if (t3.isChecked())
								Player.move(socket, player.getName(), "3", "1", true);
							else if (t4.isChecked())
								Player.move(socket, player.getName(), "4", "1", true);
						}
					}
					
					dialog.dismiss();
					SupportingFunctions.showMessage(PlayerView.this, "Success", "Player command successful", new Intent(PlayerView.this, PlayerView.class));
				} catch (IOException e) {
					SupportingFunctions.showError(PlayerView.this, e.getMessage(), new Intent(PlayerView.this, BC2ServerChoice.class));
				} catch (InvalidArguments e) {
					SupportingFunctions.showError(PlayerView.this, e.getMessage());
				} catch (BanListFull e) {
					SupportingFunctions.showError(PlayerView.this, e.getMessage());
				} catch (LogInRequired e) {
					SupportingFunctions.showError(PlayerView.this, e.getMessage(), new Intent(PlayerView.this, BC2ServerChoice.class));
				} catch (PlayerNotFound e) {
					SupportingFunctions.showError(PlayerView.this, e.getMessage());
				} catch (SetSquadFailed e) {
					SupportingFunctions.showError(PlayerView.this, e.getMessage());
				} catch (InvalidTeamId e) {
					SupportingFunctions.showError(PlayerView.this, e.getMessage());
				} catch (InvalidSquadId e) {
					SupportingFunctions.showError(PlayerView.this, e.getMessage());
				} catch (InvalidForceKill e) {
					SupportingFunctions.showError(PlayerView.this, e.getMessage());
				} catch (InvalidPlayerName e) {
					SupportingFunctions.showError(PlayerView.this, e.getMessage());
				} catch (PlayerNotDead e) {
					SupportingFunctions.showError(PlayerView.this, e.getMessage());
				} catch (SetTeamFailed e) {
					SupportingFunctions.showError(PlayerView.this, e.getMessage());
				} catch (SoldierNotAlive e) {
					SupportingFunctions.showError(PlayerView.this, e.getMessage());
				}
			}
		});
		
		cancel.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				dialog.cancel();
			}
		});

		reason.setOnFocusChangeListener(new OnFocusChangeListener() {
			boolean cleared = false;
			public void onFocusChange(View v, boolean hasFocus) {
				if (!cleared){
					reason.setText("");
					cleared = true;
				}
			}
		});
		
		dialog.show();
	
	}
	//Initializer-----/
	
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        	finish();
        	startActivity(new Intent(this, BC2ServerChoice.class));
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }
}
